﻿using AiMoInGH.Connector;
using Rhino;
using System;
using System.Collections.Generic;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Reflection;

namespace AiMoInGH.Pipeline
{
    class GrasshopperBridgeInfo
    {
        public bool HasBlockEdit { get; }
        public string[] RhinoAssemblyLocations { get; }
        public GrasshopperBridgeInfo()
        {
            RhinoAssemblyLocations = GetRhinoAssemblyLocations();
            HasBlockEdit = HasBlockEditPlugin();
        }
        public static bool HasBlockEditPlugin()
        {
            Guid id = Guid.Parse("02844255-3ba6-4b94-af8a-b4e495baff71");
            return Rhino.PlugIns.PlugIn.GetPlugInInfo(id) != null;
        }
        public string CurrentRhinoVersion => RhinoApp.Version.ToString();

        public static string[] GetRhinoAssemblyLocations()
        {
            if (IsNetCore(out DirectoryInfo rhinoCommonLoadedDirectory))
            {
                return new HashSet<string>()
                {
                    Path.Combine(rhinoCommonLoadedDirectory.Parent.FullName,"RhinoCommon.dll"),
                    typeof(Rhino.UI.ViewModel).Assembly.Location,
                    typeof(Eto.Forms.Window).Assembly.Location,
                    AiMoRhinoLoader.CurrentAiMoGHALocation,
                    typeof(Grasshopper.Instances).Assembly.Location,
                    typeof(GH_IO.GH_ISerializable).Assembly.Location,
                }.ToArray();
            }
            else
            {

                return new HashSet<string>()
                {
                    typeof(RhinoApp).Assembly.Location,
                    typeof(Rhino.UI.ViewModel).Assembly.Location,
                    typeof(Eto.Forms.Window).Assembly.Location,
                    AiMoRhinoLoader.CurrentAiMoGHALocation,
                    typeof(Grasshopper.Instances).Assembly.Location,
                    typeof(GH_IO.GH_ISerializable).Assembly.Location,
                }.ToArray();
            }
        }
        static bool IsNetCore(out DirectoryInfo rhinoCommonLoadedDirectory)
        {
            Assembly rhinoCommmon = typeof(Rhino.Geometry.Mesh).Assembly;
            rhinoCommonLoadedDirectory = new FileInfo(rhinoCommmon.Location).Directory;
            var attributesData = rhinoCommmon.GetCustomAttributesData();
            var targetFrameworkAttribute = attributesData.FirstOrDefault(a => a.AttributeType == typeof(System.Runtime.Versioning.TargetFrameworkAttribute));
            if (targetFrameworkAttribute != null)
            {
                string frameworkName = (string)targetFrameworkAttribute.ConstructorArguments[0].Value;
                return frameworkName.StartsWith(".NETCoreApp", StringComparison.OrdinalIgnoreCase);
            }
            return false;
        }
        public Bitmap GetScreenShot(Size size)
        {
            Bitmap bitmap = null;
            RhinoApp.InvokeAndWait(new Action(() =>
            {
                if (!Rhino.Commands.Command.InCommand())
                    bitmap = RhinoDoc.ActiveDoc.Views.ActiveView.CaptureToBitmap(size);
            }));
            return bitmap;
        }
    }
}
